<template>
    <div class="coupon_detail">
        <div class="coupon_info">
            <div class="left">
                <div class="left_top">
                    <div><span>{{ list.rate*100}}</span>折</div>
                    <div class="top_info">
                        <div>优惠券</div>
                        <div>{{ list.title}}</div>

                    </div>
                </div>
                <div class="left_tips">订单满<span>100</span>减<span>{{100 - list.rate*100 }}</span>元</div>
            </div>
            <div class="receive">
                <van-button v-if="!busid" type="success" @click="add(list.id)">立即领取</van-button>
                <van-button v-else-if="list.ReTF" type="danger" disabled>已领取</van-button>
                <van-button v-else type="primary" @click="add(list.id)">立即领取</van-button>    
            </div>
        </div>
        <div class="coupon_prompt">
            <div class="prompt_title">温馨提示：</div>
            <div><span>•</span>每人限领1张</div>
            <div><span>•</span>仅限量{{list.total}}张，赶快领取！</div>
            <div><span>•</span>领取后{{list.createtime_text}}至{{list.endtime_text}}有效</div>
        </div>
    </div>

    <Tabbar />
</template>

<script setup>

    const {proxy} = getCurrentInstance();//页面的this
    //获取id
    let id = proxy.$route.query.id;
    let busid = proxy.$business.value.id;
    let list = ref([]);

    onMounted(()=>{
        Info()
    })

    const Info = async ()=>{
        //发起请求
        var result = await proxy.$POST({
            url:'/coupon/info',
            params:{busid:busid,id:id}
        })
        if(!result) return false
        if(result.code == 0){
            //提醒到底了
            proxy.$fail({
                message:result.msg,
                duration:1000,
                onClose:()=>proxy.$router.go(-1)
            });
            return false
        }
       
        list.value = result.data
    }
    //领取
    const add = async(val)=>{
        if(!busid){
            proxy.$fail('请先登录!');
            return false;
        }
        //发起请求
        var result = await proxy.$POST({
            url:'/coupon/add',
            params:{busid:busid,cid:val}
        })
        if(!result) return false
        if(result.code == 0){
            //提醒到底了
            proxy.$fail(result.msg,);
            return false
        }
        //成功!
        proxy.$success(
           {
            message:result.msg,
            duration:1000,
            onClose:()=>proxy.$router.go(0)
           }
        )
    }
</script>

<style scoped>
  @import url('/assets/css/CouponInfo.css');
</style>